import sys
sys.path.append('.')
from manimlib import *
from upload_videos._2023.GMS2BeginnerTutorial.header import *
from utils.shape import *

t2c.update({
    'sprXXX': YELLOW_E, 'sXXX' : YELLOW_E, 'objXXX': BLUE_D, 'oXXX': BLUE_D
})

class Intro(IntroTemplate):
    str2 = '第3节 第一步尝试'

class Indicator1(IndicatorTemplate):
    verts = [
        [2.99, -0.77, 0],
        [5.21, -1.08, 0],
        [5.08, -2.84, 0],
        [6.43, -3.19, 0],
        [2.83, -0.74, 0],
        [5.02, -1.08, 0],
        [4.89, -1.24, 0],
        [6.24, -1.51, 0],

        [4.24, -2.10, 0],
        [6.98, -2.39, 0],
        [-4.24, 2.39, 0],
        [-1.88, 1.94, 0],
        [-2.98, 1.78, 0],
        [-1.96, 1.45, 0],
        [-2.96, 1.47, 0],
        [-1.94, 1.14, 0],
        [-4.18, 1.82, 0],
        [-3.08, 1.16, 0],

        [-4.18, 1.82, 0],
        [-3.08, 1.16, 0],
        [-6.87, 0.36, 0],
        [-5.96, 0.04, 0],

        [-2.98, 1.78, 0],
        [-1.96, 1.45, 0],

        [4.33, -1.90, 0],
        [6.95, -2.13, 0],
        [-3.38, 0.71, 0],
        [-1.99, 0.46, 0],
        [-0.24, 0.36, 0],
        [0.27, -0.18, 0],
        [4.44, -0.47, 0],
        [5.50, -0.73, 0],
    ]

class Indicator2(IndicatorTemplate):
    verts = [
        [6.68, 3.59, 0],
        [7.01, 3.32, 0],
        [-1.16, 2.86, 0],
        [0.74, 2.64, 0],
        [-4.89, 3.60, 0],
        [-4.61, 3.26, 0],
    ]

class _1(Scene):
    def construct(self) -> None:
        t1 = Text('Sprite').set_color(YELLOW_E).set_stroke(BLACK, 4, 1, True)
        t2 = Text('Object').set_color(BLUE_D).set_stroke(BLACK, 4, 1, True)
        Group(t1, t2).arrange(RIGHT, buff=LARGE_BUFF)

        self.play(Write(t1))
        self.wait(0.5)
        self.play(Write(t2))
        self.wait()
        self.play(*map(FadeOut, (t1, t2)))

        vg1 = VGroup(
            Text('Sprite（精灵）').set_color(YELLOW_E).set_stroke(BLACK, 3, 1, True),
            Text('Sprite 是一个图像（序列），你可以对其进行编辑或者导入现有的图像文件', color=GREY_D, t2c=t2c).scale(0.7),
        ).arrange(DOWN, aligned_edge=LEFT)
        vg1[1][11:15].set_color(GREY_A)

        vg2 = VGroup(
            Text('Object（物体）').set_color(BLUE_D).set_stroke(BLACK, 3, 1, True),
            Text('Object 是游戏的“活动部件”，玩家、物品、敌人等都需要它来实现', color=GREY_D, t2c=t2c).scale(0.7),
        ).arrange(DOWN, aligned_edge=LEFT)
        
        vg = VGroup(vg1, vg2).arrange(DOWN, aligned_edge=LEFT, buff=MED_LARGE_BUFF).scale(0.7).to_corner(UL)

        for g in vg:
            self.play(
                AnimationGroup(
                    FadeIn(g[0], scale=0.9), 
                    Write(g[1].set_stroke(BLACK))
                ),
                lag_ratio=0.3
            )
        self.wait()
        
        spr = ImageMobject('assets/gm/sprite.png').set_height(1).shift(DR + UP * 0.5)
        rect = Rectangle(0.5, 1).set_fill(opacity=0).set_stroke(BLUE_D, 4).shift(DOWN * 0.5)

        self.play(FadeIn(rect, scale=1.1))
        self.wait()
        self.play(rect.animate.shift(UP * 0.5), run_time=0.3)
        self.play(rect.animate.shift(DR * 0.5), path_arc=40 * DEGREES, run_time=0.3)
        self.play(rect.animate.shift(LEFT * 0.5), path_arc=230 * DEGREES, run_time=0.3)
        self.wait()
        self.play(FadeIn(spr, DOWN * 0.3), rate_func=rush_from)
        self.play(AnimationGroup(spr.animate.shift(LEFT), rect.animate.shift(DL * 0.1), lag_ratio=0.4, rate_func=rush_into))

        self.wait()

class _3(Scene):
    def construct(self) -> None:
        frame = self.camera.frame
        room = Text('Room').set_color(LIGHT_BROWN).set_stroke(BLACK, 4, 1, True).scale(2).rotate(PI / 2, LEFT)
        circle = Square3D().scale(3.6).set_color(LIGHT_BROWN, 0.3).rotate(PI / 2, LEFT)
        obj = Text('Object').scale(1.5).set_color(BLUE_D).set_stroke(BLACK, 4, 1, True).next_to(ORIGIN, UP, 0)

        self.play(Write(obj))
        self.bring_to_back(room, circle)
        self.play(FadeIn(room), FadeIn(circle), frame.animate.scale(1.3).rotate(70 * DEGREES, LEFT).rotate(20 * DEGREES, DOWN), rate_func=rush_from)
        self.wait()

class _4(Scene):
    def construct(self) -> None:
        bg = surcamera().set_stroke(width=0).set_fill('#eeeeee', 0.85)

        txts = [
            [
                '-  Sprite 是游戏的贴图素材，可以设置到 Object 上',
            '   使其展现出贴图的样子，构成游戏画面'
            ], [
                '-  Object 处理了游戏的操作逻辑，',
                '作为“活动部件”，成为 Room 这一舞台上的角色'
            ], [
                '-  暂未编写逻辑的 Object 无法进行操作'
            ]
        ]
        vg = VGroup(
            *[
                VGroup(*[
                    Text(s, base_color=GREY_D, t2c=t2c).scale(0.6)
                    for s in txt
                ]).arrange(DOWN, aligned_edge=LEFT)
                for txt in txts
            ]
        ).set_stroke(GREY_D, 1, 1, False).arrange(DOWN, buff=MED_LARGE_BUFF, aligned_edge=LEFT).to_corner(UL)

        self.play(FadeIn(bg))
        for one in vg:
            self.play(Write(one))
            self.wait()
        self.play(*map(FadeOut, (bg, vg)))

